import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
const daoName = "MinOperDiviPl";
const ButtonGroup = Button.Group;
const columns = [
    { title: '序号', dataIndex: 'XH', key: 'XH', width: 100, sorter: true, dispaly: true, filter: true },
    { title: '交易日期', key: 'TRTD', caption: "TRTD", render: data => datetime.format(data.TRTD, "yyyy-MM-dd"),  width: 120, sorter: true, dispaly: true, filter: true },
    { title: '条线', width: 85, sorter: true, key: 'TX', dataIndex: 'TXCaption', dispaly: true, filter: true },
    { title: '科目号', width: 100, sorter: true, dataIndex: 'ITNO', key: 'ITNO', dispaly: true, filter: true },
    { title: '收费名称', width: 180, sorter: true, dataIndex: 'CHNM', key: 'CHNM', dispaly: true, filter: true },
    { title: '收款金额', width: 120, sorter: true, key: 'HDCH', dataIndex: 'HDCH', dispaly: true, filter: true },
    { title: '导入结果', width: 180, sorter: false, dataIndex: 'DRJG', key: 'DRJG', dispaly: true, filter: true },
    { title: '员工工号1', width: 120, sorter: true, key: 'KHJL1', dataIndex: 'KHJL1', dispaly: true, filter: true },
    { title: '金额1', width: 120, sorter: true, key: 'JE1', dataIndex: "JE1", dispaly: true, filter: true },
    { title: '员工工号2', width: 120, sorter: true, key: 'KHJL2', dataIndex: "KHJL2", dispaly: true, filter: true },
    { title: '金额2', width: 120, sorter: true, key: 'JE2', dataIndex: "JE2", dispaly: true, filter: true },
    { title: '员工工号3', width: 120, sorter: true, key: 'KHJL3', dataIndex: "KHJL3", dispaly: true, filter: true },
    { title: '金额3', width: 120, sorter: true, dataIndex: 'JE3', key: 'JE3', dispaly: true, filter: true, canEdit: false },
    { title: '员工工号4', width: 120, sorter: true, key: 'KHJL4', dataIndex: "KHJL4", dispaly: true, filter: true },
    { title: '金额4', width: 120, sorter: true, dataIndex: 'JE4', key: 'JE4', dispaly: true, filter: true, canEdit: false },
    { title: '员工工号5', width: 120, sorter: true, key: 'KHJL5', dataIndex: "KHJL5", dispaly: true, filter: true },
    { title: '金额5', width: 120, sorter: true, key: 'JE5', dataIndex: "JE5", dispaly: true, filter: true },
    { title: '员工工号6', width: 120, sorter: true, key: 'KHJL6', dataIndex: "KHJL6", dispaly: true, filter: true },
    { title: '金额6', width: 120, sorter: true, dataIndex: 'JE6', key: 'JE6', dispaly: true, filter: true, canEdit: false },
    { title: '员工工号7', width: 120, sorter: true, key: 'KHJL7', dataIndex: "KHJL7", dispaly: true, filter: true },
    { title: '金额7', width: 120, sorter: true, dataIndex: 'JE7', key: 'JE7', dispaly: true, filter: true, canEdit: false },
];
const controls = [
    { name: 'XH', required: true, type: 'text', label: '序号', min: 1, max: 50, grid: 12 / 24 },
    { name: 'TRTD', required: true, type: 'date', label: '交易日期', dataType: "date", min: 1, max: 50, grid: 12 / 24 },
    { name: "TX", label: "条线", required: true, data: [{ value: '1', label: '公司类' }, { value: '2', label: '国际类' }, { value: '3', label: '同业类' }, { value: '4', label: '投行类' }, { value: '5', label: '零售类' }], grid: 12 / 24, type: "select" },
    { name: 'ITNO', required: false, type: 'text', label: '科目号', min: 1, max: 50, grid: 12 / 24 },
    { name: 'CHNM', required: false, type: 'text', label: '收费名称', min: 1, max: 50, grid: 12 / 24 },
    { name: 'HDCH', required: false, type: 'text', label: '收款金额', min: 1, max: 50, grid: 12 / 24 },
    {
        name: 'KHJL1', required: true, type: 'search', label: '员工工号1', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'JE1', required: true, type: 'number', label: '金额1', dataType: "number", min: 1, max: 50, grid: 12 / 24 },
    {
        name: "KHJL2", required: false, type: 'search', label: '员工工号2', min: 1, max: 50, grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: "JE2", required: false, type: 'number', label: '金额2', dataType: "number", min: 1, max: 50, grid: 12 / 24 },
    {
        name: "KHJL3", required: false, type: 'search', label: '员工工号3', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'JE3', required: false, type: 'number', label: '金额3', dataType: "number", grid: 12 / 24 },
    {
        name: "KHJL4", required: false, type: 'search', label: '员工工号4', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'JE4', required: false, type: 'number', label: '金额4', dataType: "number", grid: 12 / 24 },
    {
        name: "KHJL5", required: false, type: 'search', label: '员工工号5', min: 1, max: 50, grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: "JE5", required: false, type: 'number', label: '金额5', dataType: "number", min: 1, max: 50, grid: 12 / 24 },
    {
        name: "KHJL6", required: false, type: 'search', label: '员工工号6', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'JE6', required: false, type: 'number', label: '金额6', dataType: "number", grid: 12 / 24 },
    {
        name: "KHJL7", required: false, type: 'search', label: '员工工号7', grid: 12 / 24,
        fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'No', valueTpl: "No" }
    },
    { name: 'JE7', required: false, type: 'number', label: '金额7', dataType: "number", grid: 12 / 24 },
    { name: 'DRJG', required: false, ignore: true, type: 'text', label: '导入结果', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class MinOperDiviPLForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoName}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        let data =null
        let pTypes = popedomTypes[permID]
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="员工认定批量" importName="minOperpl" daoName="MinOperDiviPl" keys={["ID"]} paramNames={["TX"]} importMode="DeleteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
MinOperDiviPLForm = connect(mapStateToProps)(MinOperDiviPLForm)
export default MinOperDiviPLForm;

let render = (props) => React.createElement(MinOperDiviPLForm, props);
let f = new Factory()
f.set("MinOperDiviPLForm", render)
